package com.example.yiqiuser.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ fileName:LoginFilter
 * @ description:
 * @ author:xjd
 * @ createTime:2023/3/27 20:40
 * @ version:1.0.0
 */
//@WebFilter(filterName = "LoginFilter",value = "/*")
public class LoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        /*System.out.println("初始化--------------------");*/
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        /*System.out.println("-----过滤----------");*/
        //向下转型
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;

        String requestURI = request.getRequestURI();
        /*System.out.println("请求的地址为："+requestURI);*/
        if (requestURI.endsWith(".jpg")||requestURI.endsWith(".css")||
                requestURI.endsWith(".js")||requestURI.contains("/html/back/login.html")||
                requestURI.contains("/user/login")||requestURI.contains("axios.min.js.map")||
                requestURI.contains("/js/axios.min.js")){
            chain.doFilter(request,response);
        }else {
            Object empInfo = request.getSession().getAttribute("userInfoA");
            if (empInfo==null){
                response.sendRedirect("/html/back/login.html");
            }else {
                chain.doFilter(request,response);
            }
       }

    }

    @Override
    public void destroy() {
        System.out.println("销毁--------------------");
    }
}
